package com.zhiwei.controller;

import com.zhiwei.pojo.User;
import com.zhiwei.service.UserService;
import com.zhiwei.vo.PageResult;
import com.zhiwei.vo.Password;
import com.zhiwei.vo.SysResult;
import com.zhiwei.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/getMsgNum/{userId}")
    public SysResult getMsgNum(@PathVariable Integer userId){
        int i = userService.getMsgNum(userId);
        return SysResult.success(i);
    }
    @PutMapping("/cleanMsgNum/{userId}")
    public SysResult cleanMsgNum(@PathVariable Integer userId){
        User user = userService.selectById(userId);
        user.setMsgNum(0);
        int i = userService.updateById(user);
        return SysResult.success(i);
    }
    @GetMapping("/getlist")
    public SysResult getlist(){
        List<User> list = userService.getlist();
        return SysResult.success(list);
    }
    //用户登录
    @PostMapping("/login")
    public SysResult login(@RequestBody User user){
        UserInfo userInfo = userService.login(user);
        if (StringUtils.hasLength(userInfo.getToken())){
            return SysResult.success(userInfo);
        }
        return SysResult.fail();
    }
    //用户添加
    @PostMapping("/addUser")
    public SysResult addUser(@RequestBody User user){
        if (userService.addUser(user) != 0){
            return SysResult.success();
        }
        return SysResult.fail();
    }
    //查询用户是否存在
    @GetMapping("/find/{name}")
    public SysResult findUser(@PathVariable String name){
        return SysResult.success(userService.findUser(name) == null ? 0 : 1);
    }
    @GetMapping("/getById/{id}")
    public SysResult getUserById(@PathVariable Integer id){
        User user = userService.getUserById(id);
        if (user != null){
            return SysResult.success(user);
        }
        return SysResult.fail();
    }
    /*
    author: 魏浪
   url: /user/resetUser
   parameter: resetUserModel: {
         userId: window.sessionStorage.getItem("userId"),
         username: '',
         phone: '',
         email: '',
         sex: ''

      result： SysResult
     */
    @PostMapping("/resetUser")
    public SysResult updateUser(@RequestBody User user){
        userService.updateUser(user);
        return SysResult.success();
    }



    /*
    author: 魏浪
         url: /user/resetPassword
      parameter: resetPasswordModel: {
       id: '',
       oldpass: '',
      pass: '',
      checkPass: '',
     }
     */
    @PutMapping("/resetPassword")
    public SysResult resetPassword(@RequestBody Password password){
        System.out.println(password);
        User user = new User();
        user.setId(password.getId()).setPassword(password.getOldpass());
        UserInfo userInfo = userService.login(user);
        if(userInfo.getToken() == null){
            return SysResult.fail();
        }else {
            user.setId(password.getId()).setPassword(password.getPass());
            userService.resetPassword(user);
        }

        return SysResult.success();
    }


    /*
    author: 魏浪1
    parameter: queryInfo: {
          query: '',
          pageNum: 1,
          pageSize: 10
        }
       url:/user/wei/getlist
     */
    @GetMapping("/wei/getlist")
    public SysResult getUserList(PageResult pageResult){
        pageResult = userService. getUserList(pageResult);
        return SysResult.success(pageResult);
    }
    /*
    author: 魏浪
   请求路径 /user/status/{id}/{status}
   请求类型 PUT
   请求参数: 用户ID/状态值数据
    */
    @PutMapping("/wei/status/{id}/{status}")
    public SysResult updateStatus(User user){
        userService.updateStatus(user);
        return SysResult.success(user);
    }


    /*
    author: 魏浪 上传
    url:'/user/wei/addUser
    parameter:
    addUserModel: {
          username: '',
          password: '',
          password2: '',
          email:  '',
          phone:  '',
          loginName: '',
          department: ''
        }
     */
    @PostMapping("/wei/addMyUser")
    public  SysResult addMyUser(@RequestBody User user) {
        userService.addMyUser(user);
        return SysResult.success();
    }

    /*
     author: 魏浪
    url:/user/wei/${user.id}
     */
    @DeleteMapping("/wei/{id}")
    public SysResult deleteUser(@PathVariable Integer id){
        userService.deleteUser(id);
        return SysResult.success();
    }
}
